package com.Szjcyh.Until;

import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.NameValuePair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Api2 {
	
	   public static ResultModel2 buildRequest(String url,String jsonStr) throws Exception {
	    	return buildRequest(url,"", "",jsonStr,null,null);

	    }
	    
	    public static ResultModel2 buildRequest(String url,NameValuePair[] parameters,String mt_token) throws Exception {
	    	return buildRequest(url,"", "","",parameters,mt_token);

	    }
	    
	    public static ResultModel2 buildRequest(String url,NameValuePair[] parameters) throws Exception {
	    	return buildRequest(url,"", "","",parameters,null);

	    }
	    
	    public static ResultModel2 buildRequest(String url,String strParaFileName, String strFilePath,String jsonStr,NameValuePair[] parameters,String mt_token) throws Exception {

	        HttpProtocolHandler2 httpProtocolHandler = HttpProtocolHandler2.getInstance();

	        //HttpRequest request = new HttpRequest(HttpResultType.BYTES);
	        HttpRequest request = new HttpRequest(HttpResultType.STRING);
	        //设置编码集
	        request.setMethod(HttpRequest.METHOD_POST);
	        request.setCharset("UTF-8");
	        request.setUrl(url);

	        request.setParameters(parameters);
	        HttpResponse response = httpProtocolHandler.execute(request,strParaFileName,strFilePath,jsonStr,mt_token);
	        if (response == null) {
	            return null;
	        }
	        
	        String strResult = response.getStringResult();
	        System.out.println("strResult="+strResult);
	        List<ResultModel2> list=JSON.parseArray(strResult, ResultModel2.class);
	        ResultModel2 jsonResult=null;
	        if(list!=null && list.size()>0)
	        {
	        	jsonResult=list.get(0);
	        }

	        return jsonResult;
	    }
	    
	    public static NameValuePair[] generatNameValuePair(Map<String, String> properties) {
	        NameValuePair[] nameValuePair = new NameValuePair[properties.size()];
	        int i = 0;
	        for (Map.Entry<String, String> entry : properties.entrySet()) {
	            nameValuePair[i++] = new NameValuePair(entry.getKey(), entry.getValue());
	        }
	        
	        return nameValuePair;
	    }
	    
	    
	    public static String generatNameValuePair2(NameValuePair[] nameValuePair) {
	    	JSONObject jsonParam = new JSONObject(); 
	    	  
	        for (NameValuePair np:nameValuePair) {
	        	jsonParam.put(np.getName(), np.getValue());
	        }
	        return jsonParam.toString();
	    }

	    public static String getStringFromJson(JSONObject adata) {  
	        StringBuffer sb = new StringBuffer();  
	        sb.append("{");  
	        for(Object key:adata.keySet()){  
	            sb.append("\""+key+"\":\""+adata.get(key)+"\",");  
	        }  
	        String rtn = sb.toString().substring(0, sb.toString().length()-1)+"}";  
	        return rtn;  
	    }  
}
